


import CommonAction from '../../../public/actions/employing/common';
import {getQueryCondition} from '../../../../hrpub/common/utils/utils';
import {cacheTools} from 'nc-lightapp-front';

export default class TableAction extends CommonAction {
    constructor(comp) {
        super();

        this.action = comp.action;
        this.comp = comp;
    }

    // 所有action实例化之后，将按钮的map绑定一下
    didAllInstance = () => {
        this.setRowClickHandleMap();
    }

    // 获取主表数据
    getMainTableData = async ({
        conditions, 
        pageInfo, 
        pk_org, 
        orderBeginTime, 
        billstate, 
        orderEndTime, 
        oid, 
        time,
        hasSearched
    } = {}) => {
        const {props} = this.comp;
        const {search, emp, meta, dispatch, editTable} = props;

        // 保证调用方式有两种，传参，如果没有传惨就从store拿
        !conditions && (conditions = getQueryCondition(props, 'querybill'));
        !pageInfo && (pageInfo = emp.pageInfo);
        !pk_org && (pk_org = emp.orgValue ? emp.orgValue.refpk : '');
        pk_org === 'empty' && (pk_org = '');
        !orderBeginTime && (orderBeginTime = emp.orderBeginTime);
        !billstate && (billstate = emp.orderStatus);
        !orderEndTime && (orderEndTime = emp.orderEndTime);
        !oid && (oid = meta.getMeta()['querybill'].oid);
        !time && (time = emp.orderTimeRange);
        hasSearched === undefined && (hasSearched = emp.hasSearched);

        if(hasSearched === false) {
            // return;
        }

        let postData = {
            queryAreaCode: 'querybill',
            querytype: 'true',
            querycondition: {
                logic: 'and',
                conditions: []
            },
            ...conditions, 
            pageInfo: {
                ...pageInfo,
                pageIndex: pageInfo.pageIndex
            }, 
            pk_org, 
            beginTime: orderBeginTime, 
            billstate: billstate.join(','), 
            endTime: orderEndTime, 
            oid, 
            time
        };

        // 如果是用户自定义时间范围则时间范围字段置空
        postData.time === 'custom' && (postData.time = '');

        try {
            let res = await dispatch({
                type: 'emp/getMainTableData',
                payload: {
                    postData: postData
                }
            });
            if(res.success) {
                let allpks = [];
                if(res.data && res.data.data && res.data.data.list) {
                    let pageInfo = this.deepCopy(res.data.data.list.pageInfo);
                    let isApprFlow1 = res.data.isApprove
                    this.update({
                        isApprFlow: isApprFlow1,
                        pageInfo,
                        tableSelectAll: false
                    }).then((S)=>{
                        allpks = res.data.data.list.rows.map((item) => {
                            return item.values['pk_entryapply'].value;
                        })
                        editTable.setTableData('list', res.data.data.list, false);
                        editTable.showColByKey('list', 'operation')
                    });
                   
                }
                else {
                    editTable.setTableData('list', {rows: []}, false);
                }
                cacheTools.set('allpks', allpks);
            }
        }
        catch(e) {
            console.error(`getMainTableData: ${e}`);
        }
    }
    getWidgetTableData = async ({
        pageInfo, 
    } = {}) => {
        const {props} = this.comp;
        const {search, emp, meta, dispatch, editTable} = props;

        // 保证调用方式有两种，传参，如果没有传惨就从store拿
        !pageInfo && (pageInfo = emp.pageInfo);

        let postData = {
            widgetCode:"6008C0E0",
            appCode:"60092010",
            pageCode:"60092010nccloud",
            areaCode:"list",
            pageInfo: {
				pageSize: parseInt(pageInfo.pageSize),
				total: parseInt(pageInfo.total),
				totalPage:parseInt(pageInfo.totalPage),
				pageIndex:  parseInt(pageInfo.pageIndex),
			},
        };
        try {
            let res = await dispatch({
                type: 'emp/getWidgetTableData',
                payload: {
                    postData: postData
                }
            });
            if(res.success) {
                let allpks = [];

                if(res.data && res.data.list) {
                    let pageInfo = this.deepCopy(res.data.list.pageInfo);

                    this.update({
                        pageInfo,
                        tableSelectAll: false
                    });
                    allpks = res.data.list.rows.map((item) => {
                        return item.values['pk_entryapply'].value;
                    })
                    editTable.setTableData('list', res.data.list, false);
                    editTable.hideColByKey('list', 'operation')
                }
                else {
                    editTable.setTableData('list', {rows: []}, false);
                }
                cacheTools.set('allpks', allpks);
            }
        }
        catch(e) {
            console.error(`getWidgetTableData: ${e}`);
        }
    }
    // 当用户点击了全部选择按钮
    selectAll = (props, tableId, status) => {
        this.update({
            tableSelectAll: status
        });
    }

    setRowClickHandleMap = () => {
        this.rowClickHandleMap =  {
            // 删除
            'r_del': this.action.rowAct.deleteRows,
            // 编辑
            'r_edit': this.action.rowAct.editOnes,
            // 复制
            'r_copy': this.action.rowAct.copyOnes,
            // 发送offer
            'r_send_offer': this.action.offerAct.openOfferModal,
            // 接受offer
            'r_accept_offer': this.action.offerAct.acceptOffer, 
            // 拒绝offer
            'r_reject_offer': this.action.offerAct.rejectOffer,
            // 提交offer
            'r_submit_offer': this.action.offerAct.submitOffer,
            // 附件管理
            'r_file_manager': this.action.btnAct.fileManager,
            // 查看审批详情
            'r_approveinfo': this.action.rowAct.previewApproveInfo,
            // 执行
            'r_excute': this.action.rowAct.excuteOnes
        }
    }

    // 行上的操作按钮点击
    rowClickHandle = (key, record, index) => {
        let selectedRows = [record];
        console.log(key, record);
        this.update({
            rowOperator:false,
            rowsDetail:{
                record:{},
                showX:0,
                showY:0,
                index:0,
            }
        });
        this.rowClickHandleMap[key](selectedRows);
    }

    // 主页面主表格行双击事件
    doubleClick = (record, index, fProps, e) => {
        const {action} = this.comp;

        action.formAct.toBrowsePage([record]);
    }
     // 给主页面表格每行添加操作按钮
    toOperatorView=(e,record ,index)=>{
        console.log('当前行的信息',e,record,index)
        // const {props, action} = this.comp;
        // const {emp, dispatch} = props;
        this.update({
            rowOperator:true,
            rowsDetail:{
                record:record,
                showX:e.pageX,
                showY:e.pageY,
                index,
            }
        });
    }
    closeOperator=()=>{
        this.update({
            rowOperator:false,
            rowsDetail:{
                record:{},
                showX:0,
                showY:0,
                index:0,
            }
        });
    }
    // 主页面表格选中一行
    selectOneRow = (platformProps, tableId, record, index, status) => {
        const {props, action} = this.comp;
        const {editTable, emp} = props;

        let tableRows = editTable.getAllRows('list');
        let checkedRows = editTable.getCheckedRows('list');

        this.update({
            tableSelectAll: tableRows.length === checkedRows.length
        });
    }
}